package com.pbking.facebook.delegates.feed
{
	import com.adobe.serialization.json.JSON;
	import com.pbking.facebook.Facebook;
	import com.pbking.facebook.delegates.FacebookDelegate;

	/**
	 * success variable will be overwritten by parent class onComplete().   
	 * so caller should use  <i>feedSuccess</i>  instead of  <i>success</i>  to determine whether feed is published.
	 * eg. 
	 * <pre>
	 * d:PublishUserActionDelegate = e.target as PublishUserActionDelegate; 
	 * if (d.feedSuccess) {
	 *   ...
	 * }
	 * </pre>
	 * 
	 * when feedSuccess is false, it means facebook return [false] in reply.   
	 * If facebook return false (without the errorcode), it usually indicates permission issue.
	 * 
	 * @author sol wu
	 * @see http://wiki.developers.facebook.com/index.php/Feed.publishUserAction
	 */
	public class PublishUserActionDelegate extends FacebookDelegate
	{
		public var feedSuccess:Boolean = false;
		
		/**
		 * @param template_bundle_id - type is Number because id can be larger than the maximum value of int.  
		 * @param template_data - an associative array of values.  this will be converted to a JSON-encoded associative array of the values.
		 * 					an object where each one of the public variable is one of the item that should be substituted into the templates held by the specified template bundle.  
		 * 					when doing JSON.encode(template_data), it should become a JSON-encoded associative array of the values.
		 * 					{*actor*} and {*target*} are special tokens and should not be included in this array.
		 * @param target_ids - A comma-delimited list of IDs of friends of the actor, This parameter is required if one or more templates in the template bundle makes use of the {*target*} token.  
		 * 					It should only include the IDs of friends of the actor, and it should not contain the actor's ID. 
		 */
		public function PublishUserActionDelegate(facebook:Facebook, template_bundle_id:Number, 
													template_data:Object=null, target_ids:String="", 
													body_general:String="")
		{
			super(facebook);
			fbCall.setRequestArgument("template_bundle_id", template_bundle_id.toString());
			
			if (template_data != null)
			{
				fbCall.setRequestArgument("template_data", JSON.encode(template_data));
			}
			
			if (target_ids != "") { fbCall.setRequestArgument("target_ids", target_ids) };
			if (body_general != "") { fbCall.setRequestArgument("body_general", body_general) };
			fbCall.post("facebook.feed.publishUserAction");
		}

		override protected function handleResult(result:Object):void
		{
			// success variable will be overwritten by parent class onComplete().   
			// so caller should use  feedSuccess  instead of  success  to determine whether feed is published.
			// eg. d:PublishUserActionDelegate = e.target as PublishUserActionDelegate; 
			// if (d.feedSuccess) {
			//   ...
			// } 
			// if facebook return false (without the errorcode), it usually indicate permission issue.  
			feedSuccess = result.toString() == "true";
			success = feedSuccess;
		}
		
	}
}